package com.android.GernerateKeySet;

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Formatter;

public class GenerateKeyPair {

	private static PublicKey PUkey;
	private static PrivateKey PVkey;

	public PublicKey getPublicKey()
	{
		return PUkey;
	}
	
	public PrivateKey getPrivateKey()
	{
		return PVkey;
	}
	public String formatKey(Key key) {
		StringBuffer sb = new StringBuffer();
		String algo = key.getAlgorithm();
		String fmt = key.getFormat();
		Formatter f = new Formatter();
		byte[] encoded = key.getEncoded();
		sb.append("Key[algorithm=" + algo + ", format=" + fmt + ", bytes="
				+ encoded.length + ", encoded=" +key.getEncoded() + "]\n");
		if (fmt.equalsIgnoreCase("RAW")) {
			sb.append("Key Material (in hex):: ");
			// sb.append(Util.byteArray2Hex(key.getEncoded()));
			for (byte b : key.getEncoded()) {
				f.format("%02x", b);
			}
			sb.append(f.toString());
		}
		return sb.toString();
	}

	public GenerateKeyPair() throws Exception {
		KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
		kpg.initialize(512); // 512 is the keysize.
		KeyPair kp = kpg.generateKeyPair();
		PublicKey pubk = kp.getPublic();
		PrivateKey prvk = kp.getPrivate();
		//pubk.
		// System.out.println("Generated Public Key:: " + formatKey(pubk));
		// System.out.println("Generated Private Key:: " + formatKey(prvk));
		PUkey = pubk;
		PVkey = prvk;
	}
}
